Imports WinXound_Net.gVar

Public Class FormFindAndReplace


    Public mSender As SCI_EDIT

    Private mStart As Int32 = 0


    Private Sub FormFindAndReplace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If mSender.GetSelText() <> "" Then
            TextBoxFind.Text = mSender.GetSelText
        End If
        mStart = mSender.GetCurrentPos
        CheckBoxMatchWholeWord.Checked = Preferences.General.FindWholeWord
        CheckBoxMatchCase.Checked = Preferences.General.FindMatchCase
        CheckBoxReplaceFromCaret.Checked = Preferences.General.ReplaceFromCaret
    End Sub

    Private Sub FormFindAndReplace_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Preferences.General.FindWholeWord = CheckBoxMatchWholeWord.Checked
        Preferences.General.FindMatchCase = CheckBoxMatchCase.Checked
        Preferences.General.ReplaceFromCaret = CheckBoxReplaceFromCaret.Checked
        LastWordSearch = TextBoxFind.Text
        mSender.SetFocus()
    End Sub

    Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExit.Click
        Me.Close()
    End Sub

    Private Sub FormFindAndReplace_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub ButtonFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFindNext.Click

        If TextBoxFind.Text.Length Then

            Dim StringToFind As String = TextBoxFind.Text.Trim
            Dim mEnd As Int32 = mSender.GetTextLength
            Dim mFindPos As Int32 = -1
            Dim mSearchFlag As Byte = 0

            mStart = mSender.GetCurrentPos

            Try
                mSender.SetTargetStart(mStart)
                mSender.SetTargetEnd(mEnd)

                If CheckBoxMatchWholeWord.Checked Then
                    mSearchFlag += SCFIND_WHOLEWORD
                End If
                If CheckBoxMatchCase.Checked Then
                    mSearchFlag += SCFIND_MATCHCASE
                End If
                mSender.SetSearchFlags(mSearchFlag)

                mFindPos = mSender.SearchInTarget(StringToFind)
                If mFindPos > -1 Then
                    'mStart = mFindPos + 1
                    mSender.SetSelectionStart(mFindPos)
                    mSender.SetSelectionEnd(mFindPos + StringToFind.Length)
                    mSender.ScrollCaret()
                    mSender.SetFocus()
                Else
                    MessageBox.Show("Text not found", "Winxound Find and Replace", _
                                    MessageBoxButtons.OK, _
                                    MessageBoxIcon.Information)
                End If

            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub ButtonFindPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFindPrevious.Click

        If TextBoxFind.Text.Length Then

            Dim StringToFind As String = TextBoxFind.Text.Trim
            Dim mEnd As Int32 = mSender.GetTextLength
            Dim mFindPos As Int32 = -1
            Dim mSearchFlag As Byte = 0

            mStart = mSender.GetCurrentPos - 1

            Try
                mSender.SetTargetStart(mStart)
                mSender.SetTargetEnd(0)

                If CheckBoxMatchWholeWord.Checked Then
                    mSearchFlag += SCFIND_WHOLEWORD
                End If
                If CheckBoxMatchCase.Checked Then
                    mSearchFlag += SCFIND_MATCHCASE
                End If
                mSender.SetSearchFlags(mSearchFlag)

                mFindPos = mSender.SearchInTarget(StringToFind)
                If mFindPos > -1 Then
                    'mStart = mFindPos - 1
                    mSender.SetSelectionStart(mFindPos)
                    mSender.SetSelectionEnd(mFindPos + StringToFind.Length)
                    mSender.ScrollCaret()
                    mSender.SetFocus()
                Else
                    MessageBox.Show("Text not found", "Winxound Find and Replace", _
                                    MessageBoxButtons.OK, _
                                    MessageBoxIcon.Information)
                End If

            Catch ex As Exception
                wxMessageError(ex.Message, "Form Find and Replace - ButtonFindPrevious_Click Error")
            End Try

        End If

    End Sub

    Private Sub ButtonReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReplace.Click
        Try
            If mSender.GetSelText.Length Then ''''And TextBoxReplace.Text.Length Then
                mSender.ReplaceSel(TextBoxReplace.Text)
            End If

        Catch ex As Exception
            wxMessageError(ex.Message, "Form Find and Replace - ButtonReplace_Click Error")
        End Try

    End Sub

    Private Sub TextBoxFind_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxFind.KeyDown
        If e.KeyCode = Keys.Return Then
            ButtonFindNext.PerformClick()
        End If
    End Sub



    Private Sub ButtonReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReplaceAll.Click

        If TextBoxFind.Text.Length Then ''''And TextBoxReplace.Text.Length Then

            Dim StringToFind As String = TextBoxFind.Text.Trim
            Dim ReplaceString As String = TextBoxReplace.Text.Trim
            Dim mEnd As Int32 = mSender.GetTextLength
            Dim mFindPos As Int32 = -1
            Dim mSearchFlag As Byte = 0

            If CheckBoxReplaceFromCaret.Checked Then
                mStart = mSender.GetCurrentPos
            Else
                mStart = 0
            End If


            Try
                Do
                    mSender.SetTargetStart(mStart)
                    mSender.SetTargetEnd(mEnd)

                    If CheckBoxMatchWholeWord.Checked Then
                        mSearchFlag += SCFIND_WHOLEWORD
                    End If
                    If CheckBoxMatchCase.Checked Then
                        mSearchFlag += SCFIND_MATCHCASE
                    End If
                    mSender.SetSearchFlags(mSearchFlag)

                    mFindPos = mSender.SearchInTarget(StringToFind)
                    If mFindPos > -1 Then
                        mStart = mFindPos + 1
                        mSender.SetSelectionStart(mFindPos)
                        mSender.SetSelectionEnd(mFindPos + StringToFind.Length)
                        mSender.ReplaceSel(TextBoxReplace.Text)
                        'mSender.ScrollCaret()
                        'mSender.SetFocus()

                    Else
                        If mStart = 0 Then
                            MessageBox.Show("Text not found", "Winxound Find and Replace", _
                                                                    MessageBoxButtons.OK, _
                                                                    MessageBoxIcon.Information)
                        Else
                            MessageBox.Show("Text replaced", "Winxound Find and Replace", _
                                                                    MessageBoxButtons.OK, _
                                                                    MessageBoxIcon.Information)
                            mStart = 0
                        End If
                        Exit Do

                    End If
                Loop

            Catch ex As Exception
                wxMessageError(ex.Message, "Form Find and Replace - ButtonReplaceAll_Click Error")
            End Try

        End If

    End Sub





End Class